<?php

namespace app\controller\v1;

use app\model\OrderApprovalUserModel;
use app\model\OrderBomModel;
use app\model\OrderModel;
use app\validate\OrderValidate;
use think\App;
use function Sodium\add;

class Order extends System
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * Notes: 获取订单状态类型
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function statusList()
    {
        $type = input('type');
        if (empty($type)) {
            $data = OrderModel::STATUS_LIST;
        } else if ($type == 100) {
            $data = [
                [
                    [
                        'status' => OrderModel::STATUS_WAITING_MATERIAL,
                        'name' => '待领料'
                    ],
                    [
                        'status' => OrderModel::STATUS_MATERIAL_RECEIVED_IN_PRODUCTION,
                        'name' => '已领料生产中'
                    ],
                    [
                        'status' => OrderModel::STATUS_PRODUCTION_COMPLETED,
                        'name' => '生产完成'
                    ],
                    [
                        'status' => OrderModel::STATUS_WAREHOUSED,
                        'name' => '入库'
                    ],
                    [
                        'status' => OrderModel::STATUS_DELIVERY_CONFIRMED,
                        'name' => '确认交付'
                    ],
                ]
            ];
        }


        return $this->successJson($data);
    }


    /**
     * Notes: 添加订单
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function add()
    {

        $validate = new OrderValidate();
        if (!$validate->scene('add')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->add($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();

    }


    /**
     * Notes: 编辑订单
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     */
    public function edit()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('edit')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->edit($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();
    }

    /**
     * Notes: 订单详情
     * User: chenLei
     * Date: 9/9/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('info')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->info($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }


    /**
     * Notes: 发起审批
     * User: chenLei
     * Date: 9/11/25
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function initiateApproval()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('initiateApproval')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }
        $orderModel = new OrderModel();
        $res = $orderModel->initiateApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);

    }


    /**
     * Notes: 撤销审批
     * User: chenLei
     * Date: 9/11/25
     * @return \think\response\Json
     */
    public function cancel()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('cancel')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->cancel($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);

    }

    /**
     * Notes: 待我审批的订单
     * User: chenLeiƒv
     * Date: 9/14/25
     * @return \think\response\Json
     */
    public function pendingApproval()
    {
        $orderModel = new OrderApprovalUserModel();
        $res = $orderModel->pendingApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }

    /**
     * Notes: 待审核的数量
     * User: chenLei
     * Date: 11/13/25 16:20:32
     * @return \think\response\Json
     */
    public function pendingApprovalCount()
    {
        $orderModel = new OrderApprovalUserModel();
        $res = $orderModel->pendingApprovalCount($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson(['count'=>$res]);
    }


    /**
     * Notes:  已审核的订单
     * User: chenLei
     * Date: 9/14/25
     * @return \think\response\Json
     */
    public function approved()
    {
        $orderModel = new OrderApprovalUserModel();
        $res = $orderModel->approved($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson($res);
    }

    /**
     * Notes: 提交审批
     * User: chenLei
     * Date: 9/14/25
     * @return \think\response\Json|void
     */
    public function submitApproval()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('submitApproval')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderApprovalUserModel = new OrderApprovalUserModel();
        $res = $orderApprovalUserModel->submitApproval($this->params);

        if ($res === false) {
            return $this->errorJson($orderApprovalUserModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 修改技术图纸
     * User: chenLei
     * Date: 9/15/25
     * @return \think\response\Json
     */
    public function updateTechnicalRequirements()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('updateTechnicalRequirements')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->updateTechnicalRequirements($this->params);
        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 采购部门
     * User: chenLei
     * Date: 9/16/25
     * @return \think\response\Json|void
     */
    public function bomListCg()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('bomList')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderBomModel = new OrderBomModel();
        $res = $orderBomModel->bomListCg($this->params);

        if ($res === false) {
            return $this->errorJson($orderBomModel->getError());
        }
        return $this->successJson($res);
    }

    /**
     * Notes: 产品清单(采购部门)
     * User: chenLei
     * Date: 10/27/25 10:35:24
     * @return \think\response\Json
     */
    public function productListCg()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('productListCg')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderBomModel = new OrderBomModel();
        $res = $orderBomModel->productListCg($this->params);

        if ($res === false) {
            return $this->errorJson($orderBomModel->getError());
        }
        return $this->successJson($res);
    }


    /**
     * Notes: 生成采购单成功
     * User: chenLei
     * Date: 9/16/25
     * @return \think\response\Json
     */
    public function createCgOrder()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('createCgOrder')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderBomModel = new OrderBomModel();
        $res = $orderBomModel->createCgOrder($this->params);

        if ($res === false) {
            return $this->errorJson($orderBomModel->getError());
        }

        return $this->successJson();
    }


    /**
     * Notes: 确认交付
     * User: chenLei
     * Date: 10/27/25 11:56:06
     * @return \think\response\Json
     */
    public function confirmProductionCompleted()
    {
        $validate = new OrderValidate();
        if (!$validate->scene('confirmDelivery')->check($this->params)) {
            return $this->errorJson($validate->getError());
        }

        $orderModel = new OrderModel();
        $res = $orderModel->confirmProductionCompleted($this->params);

        if ($res === false) {
            return $this->errorJson($orderModel->getError());
        }

        return $this->successJson();
    }

}